package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class DamageCrowdControl extends AdjustablePanel {
    private JCheckBox cbStun;
    private JCheckBox cbSlow;
    private JCheckBox cbTrap;
    private JCheckBox cbPetrify;
    private JCheckBox cbFreeze;

    public DamageCrowdControl() {
        this(1);
    }

    public DamageCrowdControl(final int direction) {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbStun).addComponent(this.cbSlow).addComponent(this.cbTrap).addComponent(this.cbPetrify).addComponent(this.cbFreeze));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbStun)).addGroup(layout.createParallelGroup().addComponent(this.cbSlow)).addGroup(layout.createParallelGroup().addComponent(this.cbTrap)).addGroup(layout.createParallelGroup().addComponent(this.cbPetrify)).addGroup(layout.createParallelGroup().addComponent(this.cbFreeze));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbStun, this.cbSlow);
        layout.linkSize(0, this.cbStun, this.cbTrap);
        layout.linkSize(0, this.cbStun, this.cbPetrify);
        layout.linkSize(0, this.cbStun, this.cbFreeze);
        layout.linkSize(1, this.cbStun, this.cbSlow);
        layout.linkSize(1, this.cbStun, this.cbTrap);
        layout.linkSize(1, this.cbStun, this.cbPetrify);
        layout.linkSize(1, this.cbStun, this.cbFreeze);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_DMG_CROWD_CONTROL"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbStun == null) {
            this.cbStun = new JCheckBox();
        }
        this.cbStun.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_RESIST_STUN"));
        this.cbStun.setFont(fntCheck);
        if (this.cbSlow == null) {
            this.cbSlow = new JCheckBox();
        }
        this.cbSlow.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_RESIST_SLOW"));
        this.cbSlow.setFont(fntCheck);
        if (this.cbTrap == null) {
            this.cbTrap = new JCheckBox();
        }
        this.cbTrap.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_RESIST_TRAP"));
        this.cbTrap.setFont(fntCheck);
        if (this.cbPetrify == null) {
            this.cbPetrify = new JCheckBox();
        }
        this.cbPetrify.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_RESIST_PETRIFY"));
        this.cbPetrify.setFont(fntCheck);
        if (this.cbFreeze == null) {
            this.cbFreeze = new JCheckBox();
        }
        this.cbFreeze.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "DMGTYPE_RESIST_FREEZE"));
        this.cbFreeze.setFont(fntCheck);
    }

    public void clear() {
        this.cbStun.setSelected(false);
        this.cbSlow.setSelected(false);
        this.cbTrap.setSelected(false);
        this.cbPetrify.setSelected(false);
        this.cbFreeze.setSelected(false);
    }

    public void addCriteria(final SelectionCriteria criteria) {
        if (this.cbStun.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 601);
        }
        if (this.cbSlow.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 602);
        }
        if (this.cbTrap.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 603);
        }
        if (this.cbPetrify.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 604);
        }
        if (this.cbFreeze.isSelected()) {
            SelectionCriteria.addStatInfos(criteria, 605);
        }
    }
}
